package com.msb.servicemap.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.dto.DicDistrict;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.servicemap.mapper.DicDistrictMapper;
import com.msb.servicemap.remote.MapDicDistrictClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: WuWen
 * @Date: 2023/8/21 - 08 - 21 - 14:15
 * @Description: com.msb.servicemap.service
 * @version: 1.0
 */
@Service
public class DicDistrictService {

    @Autowired
    MapDicDistrictClient mapDicDistrictClient;

    @Autowired
    DicDistrictMapper dicDistrictMapper;

    public ResponseResult initDistrict(String keywords) {

        // 调用远程接口
        String district = mapDicDistrictClient.initDistrict(keywords);
        System.out.println(district);
        // 解析接口
        JSONObject districtJsonObject = JSON.parseObject(district);
        Integer status = districtJsonObject.getInteger(AmapConfigConstants.STATUS);
        if (status != 1) {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(), CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }

        // 存入数据库
        JSONArray districtsArray = districtJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
        insert(districtsArray,"0");

        return ResponseResult.success();
    }

    private void insert(JSONArray districtsArray, String parentAddressCode) {
        if (districtsArray.size() != 0) {
            for (int i = 0; i < districtsArray.size(); i++) {
                JSONObject districtJsonObject = districtsArray.getJSONObject(i);
                String addressCode = districtJsonObject.getString(AmapConfigConstants.ADDRESS_CODE);
                String addressName = districtJsonObject.getString(AmapConfigConstants.ADDRESS_NAME);
                String level = districtJsonObject.getString(AmapConfigConstants.LEVEL);
                if (level.equals("street")){
                    continue;
                }
                int levelInt = generatorLevel(level);

                // 向数据库中插入数据
                DicDistrict dicDistrict = new DicDistrict();
                dicDistrict.setAddressCode(addressCode);
                dicDistrict.setAddressName(addressName);
                dicDistrict.setLevel(levelInt);
                dicDistrict.setParentAddressCode(parentAddressCode);
                dicDistrictMapper.insert(dicDistrict);

                JSONArray sonDistrictsJsonArray = districtJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);

                insert(sonDistrictsJsonArray, addressCode);
            }
        }

    }

    private int generatorLevel(String level) {
        int levelInt = 0;
        switch (level.trim()) {
            case "country":
                levelInt = 0;
                break;
            case "province":
                levelInt = 1;
                break;
            case "city":
                levelInt = 2;
                break;
            case "district":
                levelInt = 3;
                break;
        }
        return levelInt;
    }


}
